/* drivers/video/sprdfb/lcd_nt35597_mipi.c
 *
 * Support for st7701 mipi LCD device
 *
 * Copyright (C) 2015 Spreadtrum
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */


#include "../sprdfb_chip_common.h"
#include "../sprdfb.h"
#include "../sprdfb_panel.h"

#define printk printf

#define  LCD_DEBUG
#ifdef LCD_DEBUG
#define LCD_PRINT printk
#else
#define LCD_PRINT(...)
#endif

#define MAX_DATA   10

typedef struct LCM_Init_Code_tag {
	unsigned int tag;
	unsigned char data[MAX_DATA];
}LCM_Init_Code;

typedef struct LCM_force_cmd_code_tag{
	unsigned int datatype;
	LCM_Init_Code real_cmd_code;
}LCM_Force_Cmd_Code;

#define LCM_TAG_SHIFT 24
#define LCM_TAG_MASK  ((1 << 24) -1)
#define LCM_SEND(len) ((1 << LCM_TAG_SHIFT)| len)
#define LCM_SLEEP(ms) ((2 << LCM_TAG_SHIFT)| ms)

#define LCM_TAG_SEND  (1<< 0)
#define LCM_TAG_SLEEP (1 << 1)

static LCM_Init_Code init_data[] = {
	{LCM_SEND(8), {6, 0, 0xF0,0x46,0x23,0x11,0x01,0x01}},
	{LCM_SEND(2),{0xB1,0x15}},
	{LCM_SEND(2),{0xB2,0x15}},
	{LCM_SEND(19), {17, 0, 0xB3,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00}},
	{LCM_SEND(19), {17, 0, 0xB4,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00}},
	{LCM_SEND(5), {3, 0, 0xB5,0x00,0x00}},
	{LCM_SEND(7), {5, 0, 0xB7,0x44,0x00,0x00,0x00}},
	{LCM_SEND(7), {5, 0, 0xB8,0x54,0x00,0x00,0x00}},
	{LCM_SEND(2),{0xB9,0x00}},
	{LCM_SEND(6), {4, 0, 0xC3,0x00,0x90,0x00}},
	{LCM_SEND(6), {4, 0, 0xC4,0x00,0x90,0x00}},
	{LCM_SEND(2),{0xCA,0x20}},
	{LCM_SEND(13), {11, 0, 0xCB,0x9F,0xC0,0xC7,0xEC,0x0C,0x08,0xCC,0x20,0x20,0x00}},
	{LCM_SEND(63), {61, 0, 0xD1,0x00,0x00,0x00,0x00,0x00,0x2B,0x00,0x3F,0x00,0x5E,
			       0x00,0x72,0x00,0x83,0x00,0x94,0x00,0xA1,0x00,0xD1,
			       0x00,0xF7,0x01,0x2D,0x01,0x58,0x01,0x9C,0x01,0xD6,
			       0x01,0xD8,0x02,0x16,0x02,0x63,0x02,0x94,0x02,0xD3,
			       0x02,0xFC,0x03,0x2E,0x03,0x3D,0x03,0x4D,0x03,0x5E,
			       0x03,0x71,0x03,0x86,0x03,0x9E,0x03,0xB8,0x03,0xC7}},
	{LCM_SEND(63), {61, 0, 0xD2,0x00,0x00,0x00,0x00,0x00,0x2B,0x00,0x3F,0x00,0x5E,
			       0x00,0x72,0x00,0x83,0x00,0x94,0x00,0xA1,0x00,0xD1,
			       0x00,0xF7,0x01,0x2D,0x01,0x58,0x01,0x9C,0x01,0xD6,
			       0x01,0xD8,0x02,0x16,0x02,0x63,0x02,0x94,0x02,0xD3,
			       0x02,0xFC,0x03,0x2E,0x03,0x3D,0x03,0x4D,0x03,0x5E,
			       0x03,0x71,0x03,0x86,0x03,0x9E,0x03,0xB8,0x03,0xC7}},
	{LCM_SEND(63), {61, 0, 0xD3,0x00,0x00,0x00,0x00,0x00,0x2B,0x00,0x3F,0x00,0x5E,
			       0x00,0x72,0x00,0x83,0x00,0x94,0x00,0xA1,0x00,0xD1,
			       0x00,0xF7,0x01,0x2D,0x01,0x58,0x01,0x9C,0x01,0xD6,
			       0x01,0xD8,0x02,0x16,0x02,0x63,0x02,0x94,0x02,0xD3,
			       0x02,0xFC,0x03,0x2E,0x03,0x3D,0x03,0x4D,0x03,0x5E,
			       0x03,0x71,0x03,0x86,0x03,0x9E,0x03,0xB8,0x03,0xC7}},
	{LCM_SEND(63), {61, 0, 0xD4,0x00,0x0E,0x00,0x19,0x00,0x45,0x00,0x58,0x00,0x76,
			       0x00,0x88,0x00,0x99,0x00,0xA8,0x00,0xB4,0x00,0xE0,
			       0x01,0x00,0x01,0x35,0x01,0x5E,0x01,0x9F,0x01,0xD6,
			       0x01,0xD8,0x02,0x12,0x02,0x5A,0x02,0x89,0x02,0xC5,
			       0x02,0xEE,0x03,0x1F,0x03,0x2D,0x03,0x3D,0x03,0x4E,
			       0x03,0x60,0x03,0x75,0x03,0x8D,0x03,0xA7,0x03,0xB5}},
	{LCM_SEND(63), {61, 0, 0xD5,0x00,0x0E,0x00,0x19,0x00,0x45,0x00,0x58,0x00,0x76,
			       0x00,0x88,0x00,0x99,0x00,0xA8,0x00,0xB4,0x00,0xE0,
			       0x01,0x00,0x01,0x35,0x01,0x5E,0x01,0x9F,0x01,0xD6,
			       0x01,0xD8,0x02,0x12,0x02,0x5A,0x02,0x89,0x02,0xC5,
			       0x02,0xEE,0x03,0x1F,0x03,0x2D,0x03,0x3D,0x03,0x4E,
			       0x03,0x60,0x03,0x75,0x03,0x8D,0x03,0xA7,0x03,0xB5}},
	{LCM_SEND(63), {61, 0, 0xD6,0x00,0x0E,0x00,0x19,0x00,0x45,0x00,0x58,0x00,0x76,
			       0x00,0x88,0x00,0x99,0x00,0xA8,0x00,0xB4,0x00,0xE0,
			       0x01,0x00,0x01,0x35,0x01,0x5E,0x01,0x9F,0x01,0xD6,
			       0x01,0xD8,0x02,0x12,0x02,0x5A,0x02,0x89,0x02,0xC5,
			       0x02,0xEE,0x03,0x1F,0x03,0x2D,0x03,0x3D,0x03,0x4E,
			       0x03,0x60,0x03,0x75,0x03,0x8D,0x03,0xA7,0x03,0xB5}},
	{LCM_SEND(8), {6, 0, 0xF0,0x46,0x23,0x11,0x01,0x00}},
	{LCM_SEND(10), {8, 0, 0xB2,0x00,0x9B,0x31,0x42,0x07,0xFF,0x00}},
	{LCM_SEND(2),{0xB3,0xE9}},
	{LCM_SEND(7), {5, 0, 0xBA,0x0F,0x90,0x30,0x59}},
	{LCM_SEND(7), {5, 0, 0xBB,0x05,0x69,0x09,0x02}},
	{LCM_SEND(5), {3, 0, 0xBC,0x00,0x80}},
	{LCM_SEND(9), {7, 0, 0xBD,0xC6,0x00,0x00,0x20,0x00,0x30}},
	{LCM_SEND(7), {5, 0, 0xBE,0x01,0x04,0x0F,0xFF}},
	{LCM_SEND(2),{0xBF,0x0A}},
	{LCM_SEND(5), {3, 0, 0xC0,0x02,0x80}},
	{LCM_SEND(5), {3, 0, 0xC1,0x10,0x00}},
	{LCM_SEND(5), {3, 0, 0xC2,0x18,0x07}},
	{LCM_SEND(7), {5, 0, 0xC3,0x00,0x18,0x18,0x18}},
	{LCM_SEND(7), {5, 0, 0xC5,0x01,0x01,0xC0,0x00}},
	{LCM_SEND(7), {5, 0, 0xC7,0x3D,0xFA,0x08,0x55}},
	{LCM_SEND(74), {72, 0, 0xC9,0x09,0x01,0x2A,0x02,0x04,0x08,0x00,0x09,0x40,0x04,
			       0x02,0x00,0x07,0x01,0x90,0xE0,0x01,0x03,0x03,0x03,
			       0x01,0x08,0x01,0x90,0xE0,0x00,0x04,0x02,0x00,0x01,
			       0x08,0x11,0x80,0x20,0x01,0x01,0x01,0x01,0x01,0x08,
			       0x11,0x80,0x20,0x00,0x02,0xE4,0xEA,0xE4,0xE4,0xE4,
			       0xE4,0x3F,0xCC,0xCC,0xCC,0x8A,0xCC,0x00,0x55,0x00,
			       0x00,0x00,0x00,0x00,0x02,0x15,0x16,0x02,0x16,0x16,0x16}},
	{LCM_SEND(20), {18, 0, 0xCA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			       0x00,0x00,0x00,0x00,0x00,0x00,0x00}},
	{LCM_SEND(49), {47, 0, 0xCB,0x00,0x02,0x00,0x11,0x80,0x20,0x66,0x00,0x02,0x11,
			       0x80,0x80,0x66,0x00,0x02,0x11,0x80,0x80,0x06,0x05,
			       0x03,0x00,0x05,0x05,0x00,0x04,0x00,0x00,0x00,0x00,
			       0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x66,0x01,0x03,0x66,0x01,0x03,0x00,0x12,0x0F}},
	{LCM_SEND(27), {25, 0, 0xCC,0x30,0x01,0x81,0x00,0x00,0x11,0x80,0x20,0x01,0x03,
			       0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x00,0x02,0x00,0x00}},
	{LCM_SEND(24), {22, 0, 0xCD,0x12,0x10,0x00,0x00,0x00,0x00,0x10,0x10,0x08,0x08,
			       0x31,0x00,0x00,0x10,0x12,0x10,0x81,0x40,0x00,0x00,0x01}},
	{LCM_SEND(70), {68, 0, 0xCE,0x54,0x55,0x58,0x59,0x00,0x00,0x00,0x00,0x08,0x08,
			       0x04,0x05,0x06,0x07,0x6E,0x6F,0x6A,0x6B,0x6C,0x6D,
			       0x68,0x69,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
			       0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x51,0x00,0x00,
			       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}},
	{LCM_SEND(2),{0xEE,0x71}},
	{LCM_SEND(12), {10, 0, 0xEF,0x00,0x51,0x2C,0x74,0x02,0x18,0x84,0x84,0x70}},
	{LCM_SEND(8), {6, 0, 0xFA,0x00,0x00,0x64,0x00,0x00}},
	{LCM_SEND(8), {6, 0, 0xF0,0x46,0x23,0x11,0x01,0x01}},
	{LCM_SEND(2),{0xBC,0x01}},
	{LCM_SEND(2),{0x35,0x01}},
	{LCM_SEND(5), {3, 0, 0x44,0x00,0x00}},
	{LCM_SEND(2),{0x11,0x00}},
	{LCM_SLEEP(120)},
	{LCM_SEND(2),{0x29,0x00}},
	{LCM_SLEEP(50)},
	{LCM_SEND(2),{0x51,0xFF}},
	{LCM_SEND(2),{0x53,0x2e}},
	{LCM_SEND(2),{0x55,0x00}},


	};

static LCM_Force_Cmd_Code rd_prep_code[]={
//	{0x39, {LCM_SEND(8), {0x6, 0, 0xF0, 0x55, 0xAA, 0x52, 0x08, 0x01}}},
	{0x39,{LCM_SEND(8), {6, 0, 0xFF,0xFF,0x98,0x06,0x04,0x01}}},
	{0x37, {LCM_SEND(2), {0x3, 0}}},
};

static int32_t st7701_mipi_init(struct panel_spec *self)
{
	int32_t i = 0;
	LCM_Init_Code *init = init_data;
	unsigned int tag;

	mipi_set_cmd_mode_t mipi_set_cmd_mode = self->info.mipi->ops->mipi_set_cmd_mode;
	mipi_gen_write_t mipi_gen_write = self->info.mipi->ops->mipi_gen_write;

	LCD_PRINT("sprdfb:st7701_mipi_init\n");

	mipi_set_cmd_mode();

	for(i = 0; i < ARRAY_SIZE(init_data); i++){
		tag = (init->tag >>24);
		if(tag & LCM_TAG_SEND){
			mipi_gen_write(init->data, (init->tag & LCM_TAG_MASK));
			udelay(20);
		}else if(tag & LCM_TAG_SLEEP){
			mdelay((init->tag & LCM_TAG_MASK));
		}
		init++;
	}
	return 0;
}

static uint32_t st7701_readid(struct panel_spec *self)
{
	int32_t i = 0;
	uint32_t j =0;
	LCM_Force_Cmd_Code * rd_prepare = rd_prep_code;
	uint8_t read_data[3] = {0};
	int32_t read_rtn = 0;
	unsigned int tag = 0;
	mipi_set_cmd_mode_t mipi_set_cmd_mode = self->info.mipi->ops->mipi_set_cmd_mode;
	mipi_force_write_t mipi_force_write = self->info.mipi->ops->mipi_force_write;
	mipi_force_read_t mipi_force_read = self->info.mipi->ops->mipi_force_read;
	mipi_eotp_set_t mipi_eotp_set = self->info.mipi->ops->mipi_eotp_set;

	LCD_PRINT("sprdfb:lcd_st7701_mipi read id!\n");

	mipi_set_cmd_mode();
	mipi_eotp_set(0,1);

	for(j = 0; j < 4; j++){
		rd_prepare = rd_prep_code;
		for(i = 0; i < ARRAY_SIZE(rd_prep_code); i++){
			tag = (rd_prepare->real_cmd_code.tag >> 24);
			if(tag & LCM_TAG_SEND){
				mipi_force_write(rd_prepare->datatype, rd_prepare->real_cmd_code.data, (rd_prepare->real_cmd_code.tag & LCM_TAG_MASK));
			}else if(tag & LCM_TAG_SLEEP){
				mdelay((rd_prepare->real_cmd_code.tag & LCM_TAG_MASK));
			}
			rd_prepare++;
		}
		read_rtn = mipi_force_read(0xDB, 1,(uint8_t *)read_data);
		printk("lcd_st7701_mipi read id 0xDB value is 0x%x, 0x%x, 0x%x!\n", read_data[0], read_data[1], read_data[2]);
		read_data[0] = 0xFFFF;
		if(1){
			printk("lcd_st7701_mipi read id success!\n");
			mipi_eotp_set(1,1);
			i2c_set_bus_num(3); //iwhale2 5
			read_rtn = i2c_reg_read(0x3E, 0x00);
			printk("lcd_st7701_mipi read i2c read_rtn 0x%x\n", read_rtn);
			
			//if (0x0A != read_rtn) {
				i2c_reg_write(0x3E, 0x00, 0x0F);
				i2c_reg_write(0x3E, 0x01, 0x0F);
				i2c_reg_write(0x3E, 0x03, 0x03);
				i2c_reg_write(0x3E, 0xFF, 0x80);
				mdelay(50);
			//}
			return 0x7701;
		}
	}
	mipi_eotp_set(1,1);
	return 0x7701;
}

int32_t st7701_reset(struct panel_spec *self)
{
	sprd_gpio_request(NULL, LCM_GPIO_AVDDEN);
	sprd_gpio_direction_output(NULL, LCM_GPIO_AVDDEN, 1);
	mdelay(10);

	sprd_gpio_request(NULL, LCM_GPIO_AVEEEN);
	sprd_gpio_direction_output(NULL, LCM_GPIO_AVEEEN, 1);
	mdelay(20);

	sprd_gpio_request(NULL, LCM_GPIO_RSTN);
	sprd_gpio_direction_output(NULL, LCM_GPIO_RSTN, 1);
	mdelay(5);
	sprd_gpio_direction_output(NULL, LCM_GPIO_RSTN, 0);
	mdelay(5);
	sprd_gpio_direction_output(NULL, LCM_GPIO_RSTN, 1);
	mdelay(120);

	return 0;
}

static struct panel_operations lcd_st7701_mipi_operations = {
	.panel_init = st7701_mipi_init,
	.panel_readid = st7701_readid,
	.panel_reset = st7701_reset,
};

static struct timing_rgb lcd_st7701_mipi_timing = {
	/* unit: pixel */
	.hfp = 32,
	.hbp = 32,
	.hsync = 4,//4
	.vfp = 22,
	.vbp = 40,
	.vsync = 3,//4
};

static struct info_mipi lcd_st7701_mipi_info = {
	.work_mode  = SPRDFB_MIPI_MODE_VIDEO,
	.video_bus_width = 24, /*18,16*/
	.lan_number = 4,
	.phy_feq = 500 * 1000,//4//960
	.h_sync_pol = SPRDFB_POLARITY_POS,
	.v_sync_pol = SPRDFB_POLARITY_POS,
	.de_pol = SPRDFB_POLARITY_POS,
	.te_pol = SPRDFB_POLARITY_POS,
	.color_mode_pol = SPRDFB_POLARITY_NEG,
	.shut_down_pol = SPRDFB_POLARITY_NEG,
	.timing = &lcd_st7701_mipi_timing,
	.ops = NULL,
};

struct panel_spec lcd_st7701_mipi_spec = {
	.width = 720,
	.height = 1280,
	.fps = 60,
	.type = LCD_MODE_DSI,
	.direction = LCD_DIRECT_NORMAL,
	//.is_need_reset = 1,
	//.is_need_dsc = 1,
	.info = {
		.mipi = &lcd_st7701_mipi_info
	},
	.ops = &lcd_st7701_mipi_operations,
};
